package com.augurit.common.system.service.org.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.augurit.common.dao.AcApplicationDao;
import com.augurit.common.dao.AcServerDao;
import com.augurit.common.entity.AcServer;
import com.augurit.common.system.convert.AcServerConverter;
import com.augurit.common.system.service.org.IAcServerService;
import com.augurit.common.system.web.org.form.AcServerForm;
import com.augurit.common.util.PageUtils;

/**
 * WEB服务器逻辑实现类
 */
@Service
@Transactional
public class AcServerServiceImpl implements IAcServerService {
	
	// DAO 引用
	@Autowired
	private AcServerDao acServerDao; //应用DAO类

	@Autowired
	private AcApplicationDao acApplicationDao;

	@Transactional(readOnly = true)
	public AcServerForm get(Long id){
		return acServerDao.getForm(id);
	}

	/**
	 * 保存业务字典项Form对象数组
	 * @param forms Form对象数组
	 */
	public void save(AcServerForm... forms){
		if(forms != null)
			for(AcServerForm form : forms)
				acServerDao.save(form);
	}
	
	/**
	 * 保存业务字典项Form对象数组
	 * @param forms Form对象数组
	 */
	public void saveCascade(List<AcServerForm> forms){
		if(forms != null && forms.size() > 0)
			for(AcServerForm form : forms)
				acServerDao.saveCascade(form);
	}

	/**
	 * 根据主键ID集合删除多个业务字典项对象
	 * @param ids 业务字典项主键ID集合
	 */
	public void delete(Long... ids) {
		if(ids != null)
			acServerDao.delete(ids);
	}
	
	/**
	 * 根据主键ID集合删除多个业务字典项对象
	 * @param ids 业务字典项主键ID集合
	 */
	public void deleteCascade(Long... ids) {
		if(ids != null){
			for(Long id : ids){
				
				//清除应用与Web服务器的关联项
				acApplicationDao.disAssociateWithAcServer(id);
				
				//删除本身
				acServerDao.delete(ids);
			}
		}
	}

	/**
	 * 获取所有web服务器列表
	 */
	@Transactional(readOnly = true)
	public List<AcServerForm> getAll(){
		List<AcServer> list = acServerDao.getAll();
		return AcServerConverter.convertToFormList(list);
	}
	
	/**
	 * 分页获取所有对象
	 * @param page
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<AcServerForm> getAll(Page<AcServerForm> page){

		Page<AcServer> pg = PageUtils.newPageInstance(page);

		acServerDao.getAll(pg);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<AcServerForm> list = AcServerConverter.convertToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
}
